Skip to content

⚡ Bolt: Fast O(log n) binary search for time-series backtest lookups#62

Draft
toreleon wants to merge 3 commits into
masterfrom
bolt-perf-binary-search-11057315843125864955
Draft

⚡ Bolt: Fast O(log n) binary search for time-series backtest lookups#62
toreleon wants to merge 3 commits into
masterfrom
bolt-perf-binary-search-11057315843125864955

Conversation

@toreleon

Copy link
Copy Markdown
Owner

💡 What: Replaced O(n) .filter() operations with an O(log n) binary search (findLastBarIndex) for looking up chronological time-series data.
🎯 Why: In src/agent/backtestRunner.ts, the vnindexAt and priceOverride functions are called repeatedly in a backtest loop. Previously, these functions used .filter(b => b.time <= asOf) to search for the most recent data point, causing massive array allocations and full O(n) iterations for every lookup on large market data sets.
📊 Impact: This reduces the algorithmic complexity of time-series lookups from O(n) to O(log n) and eliminates the creation of intermediate arrays. Backtest loops with fine-grained cadences over large time windows will see a drastic decrease in CPU overhead and memory allocation.
🔬 Measurement: Verified by ensuring the vitest test suite still passes. The performance improvement can be measured by running a long backtest and observing the reduced CPU and memory footprint during the backtest turn loop.


PR created automatically by Jules for task 11057315843125864955 started by @toreleon

…es lookups

This commit introduces a reusable `findLastBarIndex` utility function that performs a binary search to find the correct index in an array of chronologically sorted market data bars.

It replaces the O(n) `.filter()` array methods used in `clipBars`, `vnindexAt` and `priceOverride` with this O(log n) lookup. This completely avoids allocating large intermediate filtered arrays during backtest loops, drastically speeding up data retrieval.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules Bot and others added 2 commits June 26, 2026 15:38
…erabilities

This commit introduces a reusable `findLastBarIndex` utility function that performs a binary search to find the correct index in an array of chronologically sorted market data bars.

It replaces the O(n) `.filter()` array methods used in `clipBars`, `vnindexAt` and `priceOverride` with this O(log n) lookup. This completely avoids allocating large intermediate filtered arrays during backtest loops, drastically speeding up data retrieval.

Additionally, this commit updates `undici`, `ws`, and `cheerio` to their latest versions to fix high-severity vulnerabilities flagged by `pnpm audit` during the CI build process.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
undici 8.5.0 caused a webidl uncloneable error during tests (TypeError: webidl.util.markAsUncloneable is not a function).
Downgrading to 7.28.0 still fixes the security vulnerabilities flagged by pnpm audit while maintaining test compatibility.

Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant